package br.usp.icmc.client.controladores;

import java.util.Iterator;
import java.util.List;

import br.usp.icmc.client.dao.ClientDAO;
import br.usp.icmc.client.dao.ProviderDAO;
import br.usp.icmc.client.dao.ServiceDAO;
import br.usp.icmc.client.entidades.Client;
import br.usp.icmc.client.entidades.Provider;
import br.usp.icmc.client.entidades.Service;
import br.usp.icmc.client.exception.RegisterException;

/********************************************************************************
 * <b> <br> Projeto: Aplicativo Cliente <br><b>
 * 
 * Classe de controle do Provedor
 * 
 * @author felipe
 * 
 *******************************************************************************/
public class ControllerProvider {

	ProviderDAO providerDAO;
	Provider provider;
	Service service;
	ControllerService controllerService;
	List services;
	ServiceDAO serviceDAO;

	/****************************************************************************
	 * Construtor padrão da entidade ControllerProvider
	 ***************************************************************************/
	public ControllerProvider(){
		providerDAO = new ProviderDAO();
		provider = new Provider();
		service = new Service();
		controllerService = new ControllerService();
		serviceDAO = new ServiceDAO();
	}
	
	/*****************************************************************************
	 * Adiciona um provedor ao sistema
	 * 
	 * @param email
	 * 				Email do provedor
	 * @param fullName
	 * 			   	Nome completo
	 * @param password
	 * 				Senha
	 * @param bussinessName
	 * 				Bussiness Name do provedor
	 * @param bussinessKey
	 * 				Bussiness Key do provedor
	 * @param webSite
	 * 				Website do provedor
	 * @param adress
	 * 				Endereço do provedor
	 * @param city
	 * 				Cidade do provedor
	 * @param state
	 * 				Estado do provedor
	 * @param country
	 * 				País do provedor
	 * @param zip
	 * 				Código postal do provedor
	 * @param description
	 * 				Descrição do provedor
	 * @throws RegisterException
	 ******************************************************************************/
	public void addProvider(String email, String userName, String fullName, String password, String institution, String webSite, String country, String description) throws RegisterException{
		
		provider.setEmail(email);
		provider.setUserName(userName);
		provider.setFullName(fullName);
		provider.setPassword(password);
		provider.setInstitution(institution);
		provider.setCountry(country);
		provider.setDescription(description);
		provider.setWebSite(webSite);
		
		providerDAO.save(provider);
				
	}
	
	
	/*****************************************************************************************
	 * Edita um provedor
	 * 
	 * @param email
	 * 			Email do provedor a ser editado
	 * @param password
	 * 			Nova senha do provedor
	 * @param bussinesName
	 * 			Novo Bussiness Name
	 * @param bussinesKey
	 * 			Nova Bussiness Key
	 * @param webSite
	 * 			Novo website
	 * @param adress
	 * 			Novo endereço
	 * @param city
	 * 			Nova cidade
	 * @param state
	 * 			Novo estado
	 * @param country
	 * 			Novo país
	 * @param zip
	 * 			Novo código postal
	 * @param description
	 * 			Nova descrição
	 * @throws RegisterException
	 ********************************************************************************************/
	public void editProvider(String email, String fullName, String userName,String password, String institution, String webSite,
									String country, String description) throws RegisterException{
		
		
		provider = providerDAO.load(email);
		provider.setPassword(password);
		provider.setFullName(fullName);
		provider.setInstitution(institution);
		provider.setCountry(country);	
		provider.setDescription(description);
		provider.setUserName(userName);

		provider.setWebSite(webSite);		



		providerDAO.saveOrUpdate(provider);
	}
	

	/****************************************************************
	 * Remove um provedor do sistema
	 * 
	 * @param email
	 * 		Email do provedor a ser removido
	 ****************************************************************/
	public void removeProvider(String email){
		
		services = controllerService.listServiceProvider(email);
				
		Iterator it = services.iterator();
		while(it.hasNext()){
			service = (Service) it.next();
			controllerService.removeService(service.getIdService());
		}		
		
		providerDAO.deleteById(email);
		
		
		
	}
	
	public void contractServiceProvider(String email, Service serviceContract){
		
		provider = providerDAO.load(email);
		provider.getContractedServices().add(serviceContract);
		//serviceContract.getContractClient().add(provider);
		providerDAO.saveOrUpdate(provider);
		//serviceDAO.save(serviceContract);		
	
		
	}
	

	public void addProviderClient(String email, String userName, String fullName, String password, String institution, String webSite, String country, String description) throws RegisterException{
		
		provider.setEmail(email);
		provider.setUserName(userName);
		provider.setFullName(fullName);
		provider.setPassword(password);
		provider.setInstitution(institution);
		provider.setCountry(country);
		provider.setDescription(description);
		provider.setWebSite(webSite);
		
		providerDAO.saveOrUpdate(provider);
				
	}
	
	public Provider associaProvider(String email){
		provider = providerDAO.load(email);
		return provider;
		
	}
	

}
